<template>
	<view :class="themeName">
		<view class="integral-mall">
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
				<view class="header flex row-between white">
					<router-link to="/bundle/pages/user_sign/user_sign">
						<view class="user-integral flex">
							<!-- <u-icon class="m-r-16" :name="require('@/bundle/static/icon_integral.png')" :size="44"></u-icon> -->
							我的积分：<text class="xxl bold">{{ integral }}</text>
						</view>
					</router-link>
					<router-link to="/user/integral_rule/integral_rule"
						style="margin-left: 180rpx;border: 1rpx solid #fff;border-radius: 20rpx;padding: 4rpx 20rpx;">
						积分规则
					</router-link>
				</view>
				<image :src="result[0].img_url" class="luck_draw" v-if="isLuck==true"
					@click="$navto.navto('/bundle/pages/luck_draw/luck_draw',{id:result[0].uri})"></image>
				<view class="main" :class="isLuck==false?'mainActive':''">
					<view class="sort-bar flex bg-white">
						<view class="sort-bar-item flex-2 flex row-center" v-for="(item, index) in sortConfig"
							:key="index" @tap="handleSort(item)" :class="item.value ? 'active' : ''">
							<text>{{ item.name }}</text>
							<view class="arrow-icon flex-col col-center row-center" v-if="!item.setValue">
								<u-icon name="arrow-up-fill" :color="item.value == 'asc'? themeColor : '#333'"></u-icon>
								<u-icon name="arrow-down-fill"
									:color="item.value == 'desc'? themeColor : '#333'"></u-icon>
							</view>
						</view>
						<view class="flex-1 flex row-center" @tap="changeGoodsType">
							<u-icon :name="goodsType === 'one' ? 'grid' : 'list-dot'" :size="44"></u-icon>
						</view>
					</view>
					<view class="goods-lists" :class="{
						'goods-lists--one': goodsType === 'one'
					}">
						<view class="goods-item" v-for="(item, index) in goodsLists" :key="index">
							<router-link
								:to="`/bundle/pages/integral_goods_details/integral_goods_details?id=${item.id}`">
								<view class="goods-item-info">
									<view class="goods-image">
										<view class="image-wrap">
											<u-image :src="item.image" width="100%" height="100%"></u-image>
										</view>
									</view>
									<view class="goods-info">
										<view class="goods-name line-2">
											{{ item.name }}
										</view>
										<view class="goods-price primary m-t-10">
											<price :content="item.need_integral" mainSize="36rpx" minorSize="24rpx"
												:prefix="null" suffix="积分" />
											<price v-if="item.exchange_way === 2" :content="item.need_money"
												mainSize="36rpx" minorSize="24rpx" prefix="+" suffix="元" />
										</view>
										<view class="buy">
											兑换
										</view>
									</view>
								</view>
							</router-link>
						</view>
					</view>
				</view>

			</mescroll-body>
		</view>
	</view>
</template>

<script>
	import {
		apiIntegralGoods
	} from '@/api/integral_mall'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins";
	import {
		trottle
	} from '@/utils/tools'
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				sortConfig: [{
						name: '最新',
						type: 'sort_new',
						value: 'desc',
						setValue: 'desc'
					},
					{
						name: '积分',
						type: 'sort_integral',
						value: ''
					},
					{
						name: '兑换量',
						type: 'sort_sales',
						value: '',
						setValue: 'desc'
					}
				],
				goodsType: 'double',
				upOption: {
					noMoreSize: 2,
					auto: false,
					empty: {
						icon: 'https://cdn.zkjialan.com/img1/empty/goods.png',
						tip: "暂无商品",
					}
				},
				goodsLists: [],
				integral: '',
				sharImage: '',
				isLuck: false,
				result: ''
			}
		},
		onShareAppMessage() {
			const {
				code
			} = this.userInfo
			const share = {
				title: '提醒：您积分再不用，就清空了！',
				path: `/bundle/pages/user_sign/user_sign?invite_code=${this.userInfo.code}`,
				imageUrl: this.sharImage
			}
			return share
		},
		methods: {
			changeGoodsType() {
				this.goodsType = this.goodsType === 'one' ? 'double' : 'one'
			},
			handleSort(current) {

				console.log(current)
				this.sortConfig.forEach((item) => {
					if (current.type != item.type) {
						item.value = ''
						return
					}
					if (item.setValue) {
						item.value = item.setValue
						return
					}
					if (item.value == 'asc') {
						item.value = 'desc'
					} else {
						item.value = 'asc'
					}

				})
			},
			upCallback(page) {
				const pageNum = page.num; // 页码, 默认从1开始
				const pageSize = page.size; // 页长, 默认每页10条
				const sort = this.sortConfig.find((item) => item.value) || {}
				apiIntegralGoods({
					[sort.type]: sort.value,
					page_no: pageNum,
					page_size: pageSize
				}).then(data => {
					const {
						goods,
						integral
					} = data
					this.integral = integral
					let curPageData = goods.lists;
					let curPageLen = curPageData.length;
					if (pageNum == 1) this.goodsLists = [];
					this.goodsLists = this.goodsLists.concat(curPageData);
					this.mescroll.endBySize(curPageLen, goods.count);
				}).catch(() => {
					this.mescroll.endErr();
				})
			},
		},
		watch: {
			sortConfig: {
				handler(val) {
					this.goodsLists = []
					this.mescroll.resetUpScroll()
				},
				deep: true
			}
		},
		onLoad() {
			// 获取活动专区轮播图
			this.$request.get(`shopapi/home/slideshow_list?type=8`).then(res => {
				console.log("活动专区" + res.data.data.lists);
				this.result = res.data.data.lists.filter(item => item.uri_type === 8);
				if (this.result.length > 0) {
					this.isLuck = true;
				}
				console.log(this.result)
			})
			this.handleSort = trottle(this.handleSort, 500, this);
			this.$request.get(`shopapi/home/slideshow_list?type=17`).then(res => {
				console.log(res.data.data.lists[0].img_url)
				this.sharImage = res.data.data.lists[0].img_url;
			})
		}

	}
</script>

<style lang="scss">
	.luck_draw {
		width: 100%;
		height: 321rpx;
		margin-top: 40px;
	}
	.u-image__image{
		border-radius: 5rpx;
	}
	page {
		padding: 0;
	}

	$goods-margin: 8rpx;

	.integral-mall {
		.header {
			position: fixed;
			background-color: #f08329;
			height: 40px;
			padding: 0 24rpx;
			width: 100%;
			top: 0;
			z-index: 99;



			// padding: 22rpx 24rpx;
			// @include background_color();
		}

		.mainActive {
			margin-top: 40px;
		}

		.main {
			position: relative;

			.sort-bar {
				height: 80rpx;

				.sort-bar-item {
					height: 100%;

					&.active {
						@include font_color();
					}
				}

				.arrow-icon {
					transform: scale(0.4);
				}
			}

			.goods-lists {
				display: flex;
				flex-wrap: wrap;
				width: 670rpx;
				margin-left: 40rpx;
				margin-top: 40rpx;
				justify-content: space-between;

				.goods-item {
					position: relative;
					width: 325rpx;
					height: 350rpx;
					border-radius: 15rpx;
					margin-bottom: 40rpx;
					background-color: #fff;

					.goods-item-info {
						// margin: $goods-margin;
						border-radius: 14rpx;
						background: #fff;
						overflow: hidden;

						.goods-image {
							width: 100%;
							height: 216rpx;
							border-radius: 15rpx 15rpx 0 0;

							/deep/ .image-wrap {
								width: 100%;
								height: 216rpx;
								border-radius: 15rpx 15rpx 0 0;
							}

							.image-wrap {
								position: absolute;
								top: 0;
								left: 0;
								right: 0;
								bottom: 0;
							}
						}

						.goods-info {

							// padding: 14rpx;
							.goods-name {
								position: absolute;
								width: 300rpx;
								font-size: 25rpx;
								font-family: 'am';
								color: #141414;
								top: 240rpx;
								left: 25rpx;
								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
							}

							.goods-price {
								position: absolute;
								width: 300rpx;
								font-size: 23rpx;
								font-family: 'ar';
								color: #141414;
								top: 275rpx;
								left: 25rpx;
								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;

								/deep/.integer {
									font-size: 15px !important;
								}
							}

							.buy {
								position: absolute;
								width: 100rpx;
								height: 40rpx;
								top: 285rpx;
								right: 30rpx;
								font-size: 23rpx;
								font-family: 'ar';
								text-align: center;
								line-height: 40rpx;
								border-radius: 20rpx;
								background-color: #f08229;
								color: #fff;
							}
						}
					}
				}

				&--one {
					.goods-item {
						width: 100%;

						.goods-item-info {
							display: flex;

							.goods-image {
								width: 200rpx;
								padding-top: 200rpx;
							}

							.goods-info {
								padding: 24rpx 20rpx;
							}
						}
					}
				}
			}
		}
	}
</style>